/* Project gaigar05 int
PIC16F628A
*/

#include <pic.h>

__CONFIG(
UNPROTECT
& LVPDIS
& BORDIS
& MCLRDIS
& PWRTEN
& WDTDIS
& INTIO
);
__IDLOC(F628);

#define s RA3
#define E RB1
#define RS RA7
#define RW RA6

unsigned char b1, b2, b3, b4, b5, b6, b7, b8;
unsigned char t1, t2, t3, t4, t5, t6;
unsigned char i, j, N, M, INT;


void lcd_w_com4(unsigned char d){
RS = 0;
RW = 0;
PORTB = d << 4;
E = 1;
E = 0;
}

void lcd_w_chr4(unsigned char d){
RS = 1;
RW = 0;
PORTB = d << 4;
E = 1;
E = 0;
}

void lcd_bfck(void){
unsigned char d;

PORTB = 0;
TRISB7 = 1;
RS = 0;
RW = 1;

do{
E = 1;
d = RB7;
E = 0;
E = 1;
E = 0;
} while(d);

PORTB = 0;
TRISB7 = 0;
}

void lcd_putcom(unsigned char d){
lcd_bfck();
lcd_w_com4(d >> 4);
lcd_w_com4(d);
}

void lcd_locate(
unsigned char x,unsigned char y){
lcd_putcom((x + 0x40 * y) | 0x80);
}

void lcd_putchr(unsigned char d){
lcd_bfck();
lcd_w_chr4(d >> 4);
lcd_w_chr4(d);
}

void lcd_puts(const unsigned char *s){
while(*s) lcd_putchr(*s++);
}

void lcd_lclr(unsigned char d){
unsigned char i;

lcd_locate(0, d);
for(i = 0; i < 40; i++)
lcd_putchr(' ');
lcd_locate(0, d);
}

void lcd_putui(
unsigned int ui, unsigned char d){

unsigned char i;
unsigned char buf[5];

for(i = 0; i < 5; i++)
buf[i] = ' ';
i = 4;
do {
buf[i] = (ui % 10) + '0';
ui = ui / 10;
i--;
} while(ui > 0);

for(i = (5 - d); i < 5; i++)
lcd_putchr(buf[i]);
}


void interrupt entry(void){

if(TMR1IF){

N ++;
if(N > 19){
N = 0;

t1 = t1++;
if(t1 > 9){
t2++; t1 = 0;
}
if(t2 > 5){
t3++; t2 = 0;
}
if(t3 > 9){
t4++; t3 = 0;
}
if(t4 > 5){
t5++; t4 = 0;
}
if(t5 > 9){
t6++; t5 = 0;
}
if((t6 > 1)&&(t5 > 3)){
t5 = 0; t6 = 0;
}
}

TMR1IF = 0;
TMR1H = 11; TMR1L = 185;
}


if(INTF){
INTF = 0;

INT = 1;

b1++;

if(b1 > 9){
b2++; b1 = 0;
}
if(b2 > 9){
b3++; b2 = 0;
}
if(b3 > 9){
b4++; b3 = 0;
}
if(b4 > 9){
b5++; b4 = 0;
}
if(b5 > 9){
b6++; b5 = 0;
}
if(b6 > 9){
b7++; b6 = 0;
}
if(b7 > 9){
b8++; b7 = 0;
}
}
}


void main(void){

CMCON = 0b00000111;
T1CON = 0b00000001;
PIE1 = 0b00000001;
INTCON = 0b11010000;
TMR1IF = 0;
INTF = 0;
ei();

PR2 = 60;
T2CON = 0b00000100;
CCPR1L = 1;
CCP1CON = 0b00001100;

OPTION = 0b10000101;

PORTA = 0;
TRISA = 0b00000000;
PORTB = 0;
TRISB = 0b00000001;

TMR0 = 0; while(TMR0 < 255);
lcd_w_com4(0x03);
TMR0 = 0; while(TMR0 < 80);
lcd_w_com4(0x03);
TMR0 = 0; while(TMR0 < 2);
lcd_w_com4(0x03);
TMR0 = 0; while(TMR0 < 2);
lcd_w_com4(0x02);

lcd_putcom(0x28);
lcd_putcom(0x01);
lcd_putcom(0x0c);

TMR0 = 0;
while(TMR0 < 255);

b1 = 0; b2 = 0; b3 = 0; b4 = 0; b5 = 0; b6 = 0; b7 = 0; b8 = 0;
t1 = 0; t2 = 0; t3 = 0; t4 = 0; t5 = 0; t6 = 0;
j = 0; N = 0; M = 0; s = 0; INT = 0;


while(1){

while(TMR1IF == 1);
TMR0 = 0;
while(TMR0 < 255);

lcd_lclr(0);
lcd_locate(0, 0);
lcd_puts(" ");
lcd_putui(b8, 1); lcd_putui(b7, 1);
lcd_putui(b6, 1); lcd_putui(b5, 1);
lcd_putui(b4, 1); lcd_putui(b3, 1);
lcd_putui(b2, 1); lcd_putui(b1, 1);
lcd_puts(" count");

lcd_lclr(1);
lcd_locate(0, 1);
lcd_puts(" ");
lcd_putui(t6, 1); lcd_putui(t5, 1);
lcd_puts(":");
lcd_putui(t4, 1); lcd_putui(t3, 1);
lcd_puts(":");
lcd_putui(t2, 1); lcd_putui(t1, 1);

if(INT){
s = 0;
for(i = 0; i < 40; i++){
for(j = 0; j < 10; j++);
s = !s;
}
INT = 0;
}
}
}